package com.gzw.dms.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.gzw.dms.KJ.SelectUserActivity;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.adapter.SelectUserAdapter;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.MessageInfo;
import com.gzw.dms.bean.SendMessageInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import junit.framework.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SendMessageActivity extends BaseActivity {
    private EditText  ed_title, ed_message;
    private TextView ed_peple;
    private Button btn_send;
    private Intent mIntent;
    private MessageInfo messageInfo;
    private SendMessageInfo sendMessageInfo;
    private ProgressDialog mProgressDialog;
    private Context mContext;
    private Toolbar toolbar;
    private String ls_map = "";
    private String ls_name = "";
    private String message = "";
    private String title = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_send_message);

    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ed_message = (EditText) findViewById(R.id.ed_message);
        ed_peple = (TextView) findViewById(R.id.ed_peple);
        ed_title = (EditText) findViewById(R.id.ed_title);
        btn_send = (Button) findViewById(R.id.btn_send);
    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("消息发送");
        mIntent = getIntent();
        setSupportActionBar(toolbar);

    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new mOnClick());
        btn_send.setOnClickListener(new mOnClick());
        ed_peple.setOnClickListener(new mOnClick());

    }

    class mOnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.btn_send:
                    sendDate();
                    break;
                case R.id.ed_peple:
                    Intent mIntetn = new Intent();
                    mIntetn.setClass(mContext, SelectUserActivity.class);
                    startActivityForResult(mIntetn, 2);
                    break;

                default:
                    finish();
                    break;
            }
        }
    }

    public void sendDate(){

        if (isNull(ed_message)) {
            T.showShort(mContext, "发送内容不能为空");
            return;
        }
        if (isNull(ed_peple)) {
            T.showShort(mContext, "联系人不能为空");
            return;
        }
        title = ed_title.getText().toString().trim();
        message = ed_message.getText().toString().trim();
        ShowOrderTask signShipOrder = new ShowOrderTask();
        signShipOrder.execute();
    }
    public class ShowOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

                mProgressDialog = ProgressDialog.show(mContext, null, "正在发送...");
                mProgressDialog.setCancelable(true);
                mProgressDialog.setCanceledOnTouchOutside(false);

        }

        protected String doInBackground(String... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            map.put("orderMsg", message);
            map.put("orderMsgTitle", title);
            map.put("receiveUser",ls_map);

            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appSaveMessageInfo"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
                mProgressDialog.dismiss();

            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {
                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else if (result.indexOf("successMsg") > 0){
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    T.showShort(mContext, successMsg);
                    finish();
                }
            }
        }
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 1) {

        }else if(resultCode == 2){
            ls_map = data.getStringExtra("ls_map");
            String str =ls_map.substring(1, ls_map.length() - 1);
            ls_name = data.getStringExtra("ls_name");
            String str1 =ls_name.substring(1, ls_name.length() - 1);
           ed_peple.setText(str1);
        }

    }
}
